package com.view;

import java.util.Timer;

import ShareLife.gar.CalculateValue;
import ShareLife.gar.ToolOld;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.MotionEvent;

import com.activity.FriendTopicActivity;
import com.activity.MainActivity;
import com.activity.R;
import com.misc.AnimationStepListener;
import com.misc.AnimationTimerTask;


public class OperateFriendLayer implements AnimationStepListener {
	// constant
	static final int ANIMATION_TIME_INTERVAL = 80;
	
	// field
	final int WIDTH = 296;
	final int HEGIHT = 208;
	final int DEPTH = 20;
	double longitude[] = {116.272,106.3320,104.419,139.5331,121.2847,113.3351,120.5630,77.210,2.210};
	double latitude[] = {39.5652,29.162,30.331,35.3746,31.144,22.1020,24.1113,38.5431,48.5125};
	boolean isRunning;
	String mood[] = { "Sad", "Happy", "Fine", "Lonely", "Travlling", "Enjoying my work" };
	Operation operation;

	int xPosition;
	int yPosition;
	int startPosition[] = new int[3];
	int endPosition[] = new int[3];
	int position[] = new int[3];
	int srcImage[];
	Rect[] containerEvent;
	int internalWidth;
	Bitmap image;
	
	AppView parentLayer;
	String friendPhoneNum;
	String name;
	Bitmap avatar;
	
	
	public OperateFriendLayer(AppView parentLayer, String friendPhoneNum, String name, Bitmap avatar, int x, int y) {
		this.parentLayer = parentLayer;
		this.friendPhoneNum = friendPhoneNum;
		this.name = name;
		this.avatar = avatar;
		
		operation = new Operation();
		// TODO zyc check
		image = Bitmap.createBitmap(WIDTH, HEGIHT, Config.ARGB_4444);
		prepareLayerImage();
		// TODO zyc must quick handle below func:
		initialPosition(x, y);
		setupClickEventArea();
	}

	
	public void initialPosition(int x, int y) {
		startPosition[0] = x;
		startPosition[1] = y;
		startPosition[2] = 10;
		xPosition = (320 - WIDTH) / 2;
		yPosition = (480 - HEGIHT) / 2;
		endPosition[0] = xPosition;
		endPosition[1] = yPosition;
		endPosition[2] = WIDTH / 2;
	}

	public void prepareLayerImage() {
		Canvas canvas = new Canvas();
		canvas.setBitmap(image);
		canvas.drawColor(Color.BLACK);
		
		Paint paint = new Paint();
		paint.setColor(AppView.NAME_TITLE_COLOR);
		canvas.drawRect(100, 47, WIDTH - 6, 97, paint);
		
		paint.setTextSize(20);
		paint.setColor(Color.BLACK);
		canvas.drawText(name,100 + (WIDTH - 100 - paint.measureText(name))/2,80, paint);
		
		ToolOld.drawImage(canvas, ((BitmapDrawable) parentLayer.getActivity().getResources().getDrawable(R.drawable.up_bar)).getBitmap(), 0, 0);
		ToolOld.drawImage(canvas, ((BitmapDrawable) parentLayer.getActivity().getResources().getDrawable(R.drawable.down_bar)).getBitmap(), 0, 145);
		
		paint.setColor(AppView.PHONENUMBER_TITLE_COLOR);
		canvas.drawRect(100, 97, WIDTH - 6, 147, paint);
		
		paint.setColor(Color.BLACK);
		// TODO zyc add userOld cfg mood
		String currentMood = mood[2];
		paint.setTextSize(24);
		canvas.drawText(currentMood,100 + (WIDTH - 100 - paint.measureText(currentMood))/2,127, paint);
		
		Matrix matrix = new Matrix();
		matrix.postScale((AppView.SL / (float)avatar.getWidth()), AppView.SL / (float)avatar.getHeight());
		matrix.postTranslate(1, 47);
		canvas.drawBitmap(avatar, matrix, paint);
		
		int tempW = image.getWidth();
		int tempH = image.getHeight();
		srcImage = new int[tempW * tempH];
		image.getPixels(srcImage, 0, tempW, 0, 0, tempW, tempH);
	}

	public void setupClickEventArea() {
		containerEvent = new Rect[7];
		int tempWidth = 291 / 3;
		containerEvent[0] = new Rect(xPosition, yPosition, xPosition
				+ tempWidth, yPosition + 47);
		containerEvent[1] = new Rect(xPosition + tempWidth + 2, yPosition,
				xPosition + 2 * tempWidth, yPosition + 47);
		containerEvent[2] = new Rect(xPosition + 2 * tempWidth + 2, yPosition,
				xPosition + 3 * tempWidth, yPosition + 47);
		containerEvent[3] = new Rect(xPosition, yPosition + 49, xPosition
				+ WIDTH, yPosition + 147);
		containerEvent[4] = new Rect(xPosition, yPosition + 147, xPosition
				+ tempWidth, yPosition + HEGIHT);
		containerEvent[5] = new Rect(xPosition + tempWidth + 2,
				yPosition + 147, xPosition + 2 * tempWidth, yPosition + HEGIHT);
		containerEvent[6] = new Rect(xPosition + 2 * tempWidth + 2,
				yPosition + 147, xPosition + 3 * tempWidth, yPosition + HEGIHT);
	}

	public void drawImage(Canvas canvas, Paint aPaint) {
		if (internalWidth > 0 && internalWidth <= WIDTH / 2) {
			int destImage[] = new int[WIDTH * (HEGIHT + 40)];
			ToolOld.flipZ(destImage, srcImage, 20, internalWidth, true, WIDTH, HEGIHT);
			canvas.drawBitmap(destImage, 0, WIDTH, xPosition, yPosition - DEPTH, WIDTH, HEGIHT + (DEPTH << 1), true, aPaint);
		} else
			ToolOld.drawImage(canvas, image, xPosition, yPosition);
	}

	public void animationEffect() {
		if (!isRunning) {
			isRunning = true;
			Timer tempTimer = new Timer();
			CalculateValue.ECurveType curveType = CalculateValue.ECurveType.InOutCirc;
			AnimationTimerTask animationTimerTask = new AnimationTimerTask(startPosition, endPosition, curveType, position, 3, 200, 20, tempTimer, this);
			tempTimer.schedule(animationTimerTask, 0, ANIMATION_TIME_INTERVAL);
		}
	}

	public void AnimationStep() {
		xPosition = position[0];
		yPosition = position[1];
		internalWidth = position[2];
		parentLayer.postInvalidate();
	}

	public void AnimationFinished() {
		isRunning = false;
		parentLayer.postInvalidate();
	}

	public int getClickedRespondAreaIndex(MotionEvent e) {
		int locationX = (int) e.getX();
		int locationY = (int) e.getY();
		for (int i = 0; i < 7; i++) {
			if (containerEvent[i].contains(locationX, locationY)) {
				return i;
			}
		}
		return -1;
	}

	
	public void handleSingleTapUp(MotionEvent e) {
		switch (getClickedRespondAreaIndex(e)) {
		case 0:
			operation.DoDialer();
			break;
		case 1:
			operation.SendSMSMesage();
			break;
		case 2:
		case 3:
		case 4:
			operation.viewFriendDetail();
			break;
		case 5:
			operation.GetLocation();
			break;
		case 6:
			operation.backToGridView();
			break;
		default:
			break;
		}
	}
	
	
	
	class Operation {
		void viewFriendDetail() {
			Intent intent = new Intent(parentLayer.getActivity(), FriendTopicActivity.class);
			intent.putExtra(MainActivity.MY_PHONE_NUMBER, parentLayer.getMyPhoneNum());
			intent.putExtra(MainActivity.FRIEND_PHONE_NUMBER, Long.parseLong(friendPhoneNum));
			parentLayer.getActivity().startActivity(intent);
		}
		
		void DoDialer() {
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + friendPhoneNum));
			parentLayer.getActivity().startActivity(callIntent);
		}

		void SendSMSMesage() {
			Uri uri = Uri.parse("smsto:" + friendPhoneNum);
			Intent messageIntent = new Intent(Intent.ACTION_SENDTO, uri);
			// messageIntent.putExtra("sms_body", message);
			parentLayer.getActivity().startActivity(messageIntent);
		}

		void GetLocation() {
			/*Intent startLocationIntent = new Intent();
			startLocationIntent.setClass(mainPage, LocationActivity.class);
			Bundle landLbundle = new Bundle();
			// only simulate the location.
			Random rnd = new Random();
			int locationindex = Math.abs(rnd.nextInt()) % longitude.length;
			landLbundle.putInt("LONGITUDE", (int) (latitude[locationindex] * 1000000));
			landLbundle.putInt("LATITUDE", (int) (longitude[locationindex] * 1000000));
			startLocationIntent.putExtras(landLbundle);
			mainPage.startActivity(startLocationIntent);*/
		}

		void backToGridView() {
			parentLayer.setIsOperateFriendLayerOn(false);
			System.gc();
			parentLayer.getActivity().setContentView(parentLayer);
		}
	}
}
